/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.validation.BindException;
import org.springframework.web.bind.support.SessionAttributeStore;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vn.com.sml.SEFM.domain.User;
import vn.com.sml.SEFM.BUS.UserBUS;
/**
 *
 * @author Tunp
 */
public class LogInController extends SimpleFormController implements Controller{
    private UserBUS userBUS = new UserBUS();
    private int log_count = 0;
    
     public LogInController() {
        //Initialize controller properties here or 
        //in the Web Application Context

//        setCommandClass(User.class);
//        setCommandName("user");
//        setSuccessView("DetailUser");
//        setFormView("ListUsers");
    }
    
       
    @Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
 
                String usn = (arg0.getParameter("username"));
                String pas = (arg0.getParameter("password"));
                        
                String userfailed = null;
                
                User user = userBUS.LogIn(usn, pas);
                
                if(user.getUserId()==null)
                {
                    if(log_count != 0) {
                        userfailed = "your username or password is incorrect. Please try again!";
                    }
                    
                    ModelAndView modelAndView = new ModelAndView("LogIn");
                    modelAndView.addObject("LogInResult", userfailed);
                    log_count++;
                    return modelAndView;
                }
                else
                {
                    String LogInResult = "Hello " + user.getUsername() + "!";
                    ModelAndView modelAndView = new ModelAndView("index");
                    modelAndView.addObject("welcome", LogInResult);
                    
                    return modelAndView;
                }
	}

//     @Override
//    protected void doSubmitAction(Object command) throws Exception {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }

    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    
//    @Override
//    protected ModelAndView onSubmit(
//    HttpServletRequest request, 
//    HttpServletResponse response, 
//    Object command, 
//    BindException errors) throws Exception {
//    ModelAndView mv = new ModelAndView("DetailUser");
//    //Do something...
//    
//    return mv;
//    }
     


}
